/*
rs57.h
offer leisai cl2c series , rs57 mode driver drive function api and type define
by lunanting

-------------------------
2021-03-30 16:02:19
creat file
-------------------------
*/
#ifndef __RS57_H
#define __RS57_H

#include "../../bsp/modbus_485.h"
#include "../../bsp/base_type.h"


/***********************default define***********************/
#define RS57_cycle_pluse    10000   //一圈脉冲数


/***********************macri define***********************/

//si address
#define RS57_si1_addr       0x0145
#define RS57_si2_addr       0x0147
#define RS57_si3_addr       0x0149
#define RS57_si4_addr       0x014B
#define RS57_si5_addr       0x014D
#define RS57_si6_addr       0x014F
#define RS57_si7_addr       0x0151

//so address
#define RS57_so1_addr       0x0157
#define RS57_so2_addr       0x0159
#define RS57_so3_addr       0x015B

//si single type and mode
    //open state
#define RS57_ctrg_open      0x20    //触发命令
#define RS57_home_open      0x21    //回零信号
#define RS57_stp_open       0x21    //强制急停
#define RS57_jog_f_open     0x23    //jog 正向
#define RS57_jog_b_open     0x24    //jog 反向
#define RS57_pl_open        0x25    //正向限位
#define RS57_nl_open        0x26    //反向限位
#define RS57_org_open       0x27    //原点信号
#define RS57_add0_open      0x28    //路径地址0
#define RS57_add1_open      0x29    //路径地址1
#define RS57_add2_open      0x2a    //路径地址2
#define RS57_add3_open      0x2b    //路径地址3
#define RS57_enable_open    0x08    //使能
#define RS57_clr_open       0x07    //报警清除
    //close state
#define RS57_ctrg_close      0xa0    //触发命令
#define RS57_home_close      0xa1    //回零信号
#define RS57_stp_close       0xa2    //强制急停
#define RS57_jog_f_close     0xa3    //jog 正向
#define RS57_jog_b_close     0xa4    //jog 反向
#define RS57_pl_close        0xa5    //正向限位
#define RS57_nl_close        0xa6    //反向限位
#define RS57_org_close       0xa7    //原点信号
#define RS57_add0_close      0xa8    //路径地址0
#define RS57_add1_close      0xa9    //路径地址0
#define RS57_add2_close      0xaa    //路径地址0
#define RS57_add3_close      0xab    //路径地址0
#define RS57_enable_close    0x88    //使能
#define RS57_clr_close       0x87    //报警清除

//so single type and mode
    //open state
#define RS57_cmd_ok_open    0x20    //指令完成
#define RS57_mc_ok_open     0x21    //路径完成
#define RS57_home_ok_open   0x22    //回零完成
#define RS57_alm_open       0x25    //报警状态
#define RS57_brk_open       0x24    //抱闸状态
#define RS57_pend_open      0x23    //到位状态
    //close state
#define RS57_cmd_ok_close   0xa0    //指令完成
#define RS57_mc_ok_close    0xa1    //路径完成
#define RS57_home_ok_close  0xa2    //回零完成
#define RS57_alm_close      0xa5    //报警状态
#define RS57_brk_close      0xa4    //抱闸状态
#define RS57_pend_close     0xa3    //到位状态


//pr 控制参数 寄存器地址
#define RS57_pr_set_addr        0x6000  //pr 控制设置
#define RS57_pr_ctrl_addr       0x6002  //pr 控制输入
#define RS57_pr_max_H           0x6006  //软件正限位高位
#define RS57_pr_max_L           0x6007  //软件正限位地位
#define RS57_pr_min_H           0x6008  //软件负限位高位
#define RS57_pr_min_L           0x6009  //软件负限位地位
#define RS57_pr_back_addr       0x600a  //回零模式
#define RS57_pr_stop_to         0x600d  //回零后去指定位置
#define RS57_pr_back_speed      0x600f  //回零速度配置
#define RS57_pr_location        0x602c  //电机位置

//set state
#define RS57_state_set          0x1801  //控制字

//warn state
#define RS57_now_warn           0x2203  //当前报警

//pr 路径参数地址
#define RS57_route0_mode_addr               0x6200  //路径0 模式
#define RS57_route0_locationH_addr          0x6201  //路径0 位置高位
#define RS57_route0_locationL_addr          0x6202  //路径0 位置地位
#define RS57_route0_speed_addr              0x6003  //路径0 速度
#define RS57_route0_acc_addr                0x6204  //路径0 加速度
#define RS57_route0_dec_addr                0x6205  //路径0 减速度
#define RS57_route0_wait_addr               0x6206  //路径0 停留时间

#define RS57_route_0        0x6200  //路径0 配置首地址
#define RS57_route_1        0x6208  //路径0 配置首地址
#define RS57_route_2        0x6210  //路径0 配置首地址
#define RS57_route_3        0x6218  //路径0 配置首地址
#define RS57_route_4        0x6220  //路径0 配置首地址
#define RS57_route_5        0x6228  //路径0 配置首地址



/************************type define**************************/
// move  type
typedef enum{
    rs57_moveType_0=0x10,       //route 0
    rs57_moveType_1,
    rs57_moveType_2,
    rs57_moveType_3,
    rs57_moveType_back=0x20,    //回零
    rs57_moveType_setZero,      //手动设零
    rs57_moveType_stop=0x40,    //急停
}RS57_move_type;

//state set
typedef enum{
    rs57_clear_now_warn=0x1111,     //清除当前报警
    rs57_clear_old_warn=0x1122,     //清除历史报警
    rs57_save_all_param=0x2211,     //保存所以参数
    rs57_init_param=0x2222,         //初始化电机参数
    rs57_recover=0x2233,            //所以参数恢复出厂
    rs57_encoder_checkzero=0x4411,  //编码器校零
    rs57_jog_fw=0x4001,             //jog 正转 需50ms间隔
    rs57_jog_bk=0x4002,             //jog 反转 需50ms间隔
}RS57_set;


//si io
typedef struct{
    int id;         //si io port,board id
    int si1_org;    //si1 pin
    int si2_stop;   //si2 pin
    int si3_en;     //si3 pin
    int si4_clr;    //si4 pin
    int si5_addr0;  //si5 pin
    int si6_addr1;  //si6 pin
    int si7_ctrg;   //si7 pin
}RS57_si;

//so io
typedef struct{
    int id;             //so io port,io board id
    int so1_mc_ok;     //so1 路径完成状态
    int so2_brk;      //so2  brk 抱闸
    int so3_alm;        //so3
}RS57_so;

//hold io
typedef struct{
    u8 id;  //board id
    u8 pin; //board out pin
    bool touch;   //touch io status
    u8 status;  //hold status
}RS57_hold;

//limit io
typedef struct{
    u8 id;  //board id
    int max;    //max io pin
    int min;    //min io pin
    int status;     //限位状态，-1= min touch，0= nothing,1= max touch,-2= min and max all touch
}RS57_limit;


//motor status
typedef struct{
    int brk;        //抱闸状态,1=close,0=open
    int mc_ok;      //路径完成状态，1=执行中，0=完成
    int alm;        //报警状态，1=正常，0=有错误
    int warn_code;  //warn code
}RS57_status;

//motor strcture property
typedef struct{
    float Kr;   //减速机  主动/从动
    float Kd;   //螺距 mm
}RS57_structure;


//rs57 motor
typedef struct{
    int init;   //init state
    int port;   //modbus port
    int speed;  //modbus speed
    int id;     //modbus slave id
    int modbus; //modbus usefull,1=normal,0=disable
    int enable; //motor enable state
    int location_mm;   //location mm or angle degree
    int location_pluse; //pluse
    int route;  //motor route num
    int dir;    //move direction,-1=revert,0=notmove,1=forwad,-2=checkzero
    RS57_si si; //si port
    RS57_so so; //so port
    RS57_status status; //motor status
    RS57_structure structure;   //structure attrubute
    RS57_hold hold; //hold brake
    RS57_limit limit;   //limit
}RS57;


// rs57 pr route mode   0x6200 pr9.00
typedef struct{
    u8 type;    //动作类型，0=无动作，1=位置定位，2=速度运行，3=回零
    u8 ins;     //插断，0=不插断，1=插断(目前固定插断)
    u8 ovlp;    //重叠，0=不重叠，1=重叠
    u8 mode;    //定位模式。0=绝对位置，1=相对指令，2=相对电机
    u8 jump_to; //跳转目标路径 0~15
    u8 jump;    //jump 0=不跳转，1=跳转
}RS57_mode;

//rs57 route attribute
typedef struct{
    RS57_mode mode;     //route mode
    u32 target;         //target location pluse
    u16 speed;          //speed r/min
    u16 acc;            //acc ms/Krpm
    u16 dec;            //dec ms/Krpm
    u16 wait_time;      //stop wait time
    u16 action;         //same to pr8.02, 仅pr0有效，动作触发
}RS_route_attr;

typedef enum{
    MIRROR_close = 0,
    MIRROR_open = 1,
    MIRROR_unknown = 2
}MIRROR_state;

/************************************function extern************************/
extern RS57 *xp_rs57_new(int port,int speed,int id);    //rs57 new a object
extern int xp_rs57_si_config(RS57 *motor,int id,int si[8]); //config si
extern int xp_rs57_so_config(RS57 *motor,int id,int so1,int so2,int so3);   //config so
extern RS57 *xp_rs57_motor_get(int id); //get motor object from modbus id
extern int xp_rs57_hold_config(RS57 *motor,int id,int pin,int touch);   //config motor holding brake io
extern int xp_rs57_limit_io_config(RS57 *motor, int id,int max_pin,int min_pin);   //config limit io

//io control motor
extern int xp_rs57_en_set(RS57 *motor,int sta); //enable motor
extern int xp_rs57_route_set(RS57 *motor,int num);  //set run route
extern int xp_rs57_clear(RS57 *motor);  //clear warn
extern int xp_rs57_origin_go(RS57 *motor);  //set goto origin single
extern int xp_rs57_stop(RS57 *motor);   //fast stop
extern int xp_rs57_hold_set(RS57 *motor,int sta);   //hold brake set

//cmd config
extern int xp_rs57_control_config(const RS57 *motor,int ctrg,
                                int limit,int back,int ctrg_type); //control config
extern int xp_rs57_limit_config(const RS57 *motor,u32 max,u32 min); //set soft limit 
extern int xp_rs57_back_config(const RS57 *motor,u8 dir,u32 stop_to);   //back zero mode config
extern int xp_rs57_backSpeed_config(const RS57 *motor,u16 speedH,u16 speedL,
                                    u16 acc_t,u16 dec_t);   //back speed config
extern int xp_rs57_route_config(const RS57 *motor,const u8 route,
                                const RS_route_attr *attr);    //config motor route attribute
extern int xp_rs57_route_target_updata(const RS57 *motor,const u8 route,
                                        const u32 target);  //config route target
extern void xp_rs57_structure_config(RS57 *motor,float kr,float kd);    //config motor structure param

// cmd set
extern int xp_rs57_control_set(const RS57 *motor,RS57_move_type type);  //control set
extern int xp_rs57_state_set(const RS57 *motor,const RS57_set type);    //set motor state 
extern int xp_rs57_cmd_move(RS57 *motor,int dis);   //cmd move 

//cmd status
extern int xp_rs57_location_get(const RS57 *motor); //motor location get
extern int xp_rs57_now_wran_get(const RS57 *motor); //get now warn code


//convert data
extern int xp_rs57_dis_to_pluse(const RS57 *motor,const int dis);   //convert distance to pluse

//motor status check


//demo
extern int xp_rs57_putter_init(int port,int speed,int id); //demo init
extern int xp_rs57_putter_recover();
extern int xp_rs57_putter_move(u8 route);
extern int xp_rs57_putter_checkZero(void);
extern bool xp_read_putter_mc_ok_flag(void);
extern bool xp_read_putter_alm_flag(void);

//mirror demo
extern int xp_rs57_mirror_init(int port,int speed,int id);  //mirror demo init
extern int xp_rs57_mirror_recover();
extern int xp_rs57_mirror_route_set(int id,u8 route);  //mirror demo route move 
extern int xp_rs57_mirror_move(int id,int angle);  //mirror demo move
extern int xp_mirror_locker_set(int id,bool sta);   //mirror locker set
extern int xp_mirrors_locker_set(int id,bool sta);  //mirrors locker set
extern int xp_mirrors_route_move(int id,u8 route);  //mirrors route move
extern int xp_mirrors_move(int id,int angle);   //mirrors move to the angle
extern int xp_mirrors_checkZero(void);
extern MIRROR_state xp_mirrors_get_state(void);
extern bool xp_mirrors_move_by_jog(bool isOpen);
#endif
//end of the file
